package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.utils.JdbcUtils;
import com.test.utils.Msg;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class AdminServlet
 * 管理员登录
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub


		System.out.println("我进来了");
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
 
        String username = request.getParameter("username");       //账号   
        String password = request.getParameter("password");       //密码
       
        JdbcUtils jdbcUtils = new JdbcUtils();  //数据库连接工具类
        try {
			Connection conn = jdbcUtils.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "select * from admin where username = '"+username+"' and password= '"+password+"'";
			ResultSet user =  stmt.executeQuery(sql); //执行sql语句
			if(user.next()) {
				out.println(JSONObject.fromObject(Msg.success()));
			}else {
				out.println(JSONObject.fromObject(Msg.fail()));
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
